﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;

namespace JapaneseMusicHtmlCreator
{
    /// <summary>
    /// PageInformation.xaml 的交互逻辑
    /// </summary>
    

    public partial class PageInformation : Page
    {
        private MainWindow mainWindow;

        public PageInformation(MainWindow mainWindow)
        {
            InitializeComponent();
            this.mainWindow = mainWindow;
        }

        private void btnPrev_Click(object sender, RoutedEventArgs e)
        {
          
                SaveMemory();
                mainWindow.NavigateBackward(this, mainWindow.pageWelcome);
    
        }

        private void SaveMemory()
        {
            mainWindow.musicArtist = tbMusicArtist.Text;
            mainWindow.musicPath = tbMusicPath.Text;
            mainWindow.musicTitle = tbMusicTitle.Text;
            string[] temp = mainWindow.musicPath.Split('.');
            mainWindow.musicType = temp[temp.Length - 1];
   
        }
        private void Save()
        {
            SaveMemory();
            mainWindow.pageKashiEdit.tbMusicTitle.Text = mainWindow.musicTitle;
            mainWindow.pageKashiEdit.tbMusicArtist.Text = mainWindow.musicArtist;
           
          
            if (!string.IsNullOrEmpty(mainWindow.musicPath))
            {
                mainWindow.mediaPlayer.Open(new Uri(mainWindow.musicPath, UriKind.Absolute));
                mainWindow.mediaPlayer.MediaOpened += mediaPlayer_MediaOpened;
                mainWindow.mediaPlayer.MediaFailed += mediaPlayer_MediaFailed;
                mainWindow.pageKashiEdit.tbKashi.Focus();
                
            }
            
        }

        void mediaPlayer_MediaFailed(object sender, ExceptionEventArgs e)
        {
            System.Windows.MessageBox.Show("不支持的媒体类型", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            mainWindow.mediaPlayer.Close();
        }

        void mediaPlayer_MediaOpened(object sender, EventArgs e)
        {
            mainWindow.NavigateForward(this, mainWindow.pageKashiEdit);
            mainWindow.mediaPlayer.MediaOpened -= mediaPlayer_MediaOpened;
        }
        private void btnNext_Click(object sender, RoutedEventArgs e)
        {

             if (string.IsNullOrEmpty(tbMusicPath.Text)||string.IsNullOrEmpty(tbMusicArtist.Text)||string.IsNullOrEmpty(tbMusicTitle.Text))
            {
                System.Windows.MessageBox.Show("请将信息填写完整！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            else
            {
                Save();
            }


        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = Environment.CurrentDirectory; 
            ofd.Filter = "音乐文件|*.mp3;*wav";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                if (!string.IsNullOrEmpty(ofd.FileName))
                {
                    bool b = false;
                    tbMusicPath.Text = ofd.FileName;
                    string[] t = tbMusicPath.Text.Split('\\');
                    string s = t[t.Length - 1];
                    string str = s.Substring(0, s.Length - 4);
                    string[] temp = str.Split('-');
                    if (temp.Length != 1)
                    {
                        int len = temp.Length - 1;
                        tbMusicTitle.Clear();
                        tbMusicArtist.Text = temp[0].Trim();
                        for (int i = 1; i < len; i++)
                        {
                            tbMusicTitle.Text += temp[i].Trim() + "-";
                        }
                        tbMusicTitle.Text += temp[len].Trim();
                        b = true;
                    }
                       
                    
                    if (!b)
                    {
                        System.Windows.MessageBox.Show("未能自动识别歌曲名称，请手动输入！", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    tbMusicPath.Focus();
                    tbMusicPath.Select(tbMusicPath.Text.Length, 0);
                   
                }
            }
        }
    }
}
